﻿//-----------------------------------------------------------------------
// <copyright file="WebRequestFactory.cs" company="Redmist">
//     Copyright (c) Damien T Smith. All rights reserved.
// </copyright>
// <author>Damien Smith</author>
//-----------------------------------------------------------------------

namespace Redmist.UsageMonitor.WebIO
{
    using System.IO;
    using System.Net;
    using System.Text;

    /// <summary>A factory for setting up web requests for the aaNet web site.</summary>
    public static class WebRequestFactory
    {
        /// <summary>A System.String representing the (HTTPS) URL of the aaNet member's login page.</summary>
        private const string LoginUrl = "https://www.aanet.com.au/members.html";

        /// <summary>A System.String representing the URL of the usage statistics page.</summary>
        private const string UsageUrl = "https://www.aanet.com.au/usage.html?days=120";

        /// <summary>A System.String representing the HTTP header content type for HTTP requests.</summary>
        private const string ContentType = "application/x-www-form-urlencoded";

        /// <summary>The response timeout in milliseconds for HTTP requests.</summary>
        private const int Timeout = 15000;

        /// <summary>Sets up the initial request to the member login web page.</summary>
        /// <param name="postData">A System.String containing the post data used for login authentication.</param>
        /// <returns>A Redmist.UsageMonitor.WebIO.IHttpWebRequest representing a HTTP request with the necessary login parameters applied.</returns>
        public static IHttpWebRequest CreateInitialWebRequest(string postData)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(LoginUrl);
            request.Timeout = Timeout;
            request.ContentType = ContentType;
            request.ContentLength = postData.Length;
            request.Method = "POST";
            request.AllowAutoRedirect = false;
            byte[] postBytes = Encoding.ASCII.GetBytes(postData);
            using (Stream requestStream = request.GetRequestStream())
            {
                requestStream.Write(postBytes, 0, postBytes.Length);
            }

            return new UsageWebRequest(request);
        }

        /// <summary>Sets up a HTTP request to access the usage data page.</summary>
        /// <param name="sessionCookie">A System.Net.Cookie containing session information to allow access to the usage page.</param>
        /// <returns>A Redmist.UsageMonitor.WebIO.IHttpWebRequest representing a HTTP request with the necessary session ID.</returns>
        public static IHttpWebRequest CreateUsageRequest(Cookie sessionCookie)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(UsageUrl);
            request.CookieContainer = new CookieContainer();
            request.CookieContainer.Add(sessionCookie);
            return new UsageWebRequest(request);
        }
    }
}
